package com.example.imtoken.modules.main.wallet.manage.wallet

import android.content.Intent
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ClickUtils
import com.example.imtoken.R
import com.example.imtoken.core.OnAdapterClickActivityListener
import com.example.imtoken.core.base.BaseActivity
import com.example.imtoken.databinding.ActivityManageWalletBinding
import com.example.imtoken.modules.main.wallet.add_token.AddTokenActivity
import com.example.imtoken.modules.main.wallet.manage.ManageActivity
import com.example.imtoken.modules.main.wallet.manage.wallet.add.AddWalletActivity

/**
 * 管理钱包
 * @property manageWalletTypeAdapter ManageWalletTypeAdapter
 * @property manageWalletAdapter ManageWalletAdapter
 */
class ManageWalletActivity :
    BaseActivity<ManageWalletViewModel, ActivityManageWalletBinding>(ManageWalletViewModel::class.java) {

    private lateinit var manageWalletTypeAdapter: ManageWalletTypeAdapter
    private lateinit var manageWalletAdapter: ManageWalletAdapter

    override fun initLayoutId(): Int {
        return R.layout.activity_manage_wallet
    }

    override fun init() {
        dataBinding.include4.tvHeadTitles.text = getString(R.string.manageWallet)
        dataBinding.include4.ivHeadBack.setOnClickListener {
            this.finish()
        }
        initManageWalletTypeAdapter()
        initManageWalletAdapter()
        initObserve()
        initClick()

        viewModel.getManageWalletTypeData()
    }

    private fun initClick() {
        ClickUtils.applyGlobalDebouncing(dataBinding.linearLayout6) {
            ActivityUtils.startActivity(AddWalletActivity::class.java)
        }
    }

    private fun initManageWalletAdapter() {
        manageWalletAdapter = ManageWalletAdapter(R.layout.item_manage, 2)
        dataBinding.rvManageWallet.layoutManager = LinearLayoutManager(this)
        dataBinding.rvManageWallet.adapter = manageWalletAdapter
        manageWalletAdapter.setOnAdapterClickActivityListener(object :
            OnAdapterClickActivityListener {
            override fun click(ob: Any?) {
                if (ob == null) {
                    //跳转添加代币
                    val intent = Intent(this@ManageWalletActivity, AddTokenActivity::class.java)
                    intent.putExtra("addTokenType", 3)
                    startActivityForResult(intent, 0)
                } else {
                    val intent = Intent(this@ManageWalletActivity, ManageActivity::class.java)
                    intent.putExtra("key", ob as String)
                    startActivityForResult(intent, 0)
                }
            }
        })
    }

    private fun initManageWalletTypeAdapter() {
        manageWalletTypeAdapter = ManageWalletTypeAdapter(R.layout.item_manage_wallet_type)
        dataBinding.rvSelectWallet.layoutManager = LinearLayoutManager(this)
        dataBinding.rvSelectWallet.adapter = manageWalletTypeAdapter
        manageWalletTypeAdapter.setOnItemClickListener { _, _, position ->
            viewModel.updateSelectData(position)
        }
    }

    private fun initObserve() {
        viewModel.getManageWalletTypeLiveData().observe(this) {
            manageWalletTypeAdapter.setList(it)
        }
        viewModel.getManageWalletLiveData().observe(this) {
            manageWalletAdapter.setList(it)
            dataBinding.rvManageWallet.scrollToPosition(0)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == 200) {
            viewModel.getManageWalletData()
        }
    }

}